﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MVCCentral.Framework.Logger
{
    /// <summary>
    /// The LogFactory class implements a pseudo-factory pattern and 
    /// a Strategy pattern to derive the proper Logger subclass.
    /// </summary>
    class LogFactory
    {
        private Dictionary<LoggingStrategy, ILogger>
            _strategyList = new Dictionary<LoggingStrategy, ILogger>();

        public LogFactory()
        {
            DefineStrategies();
        }

        /// <summary>
        /// To return a single Logger.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public ILogger CreateLogger(LoggingStrategy type)
        {
            ILogger logger = null;
            logger = _strategyList[type];

            return logger;
        }

        /// <summary>
        /// To return a list of Loggers.
        /// </summary>
        /// <param name="types"></param>
        /// <returns></returns>
        public List<ILogger> CreateLoggers(List<LoggingStrategy> types)
        {
            List<ILogger> loggerList = new List<ILogger>();

            ILogger logger = null;

            foreach (LoggingStrategy item in types)
            {
                logger = _strategyList[item];
                loggerList.Add(logger);
            }

            return loggerList;
        }

        private void DefineStrategies()
        {
            // Add more Logger classes that implement ILogger 
            // here for each LoggingStrategy.
            _strategyList.Add(LoggingStrategy.FileError, new FileLogger());
            _strategyList.Add(LoggingStrategy.FileTrace, new FileLogger());
            _strategyList.Add(LoggingStrategy.Sql, new SqlLogger());
            _strategyList.Add(LoggingStrategy.Email, new EmailLogger());
        }
    }
}
